package com.common.baselibrary.common;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import com.common.baselibrary.ApplicationManager;
import com.common.baselibrary.log.LogUtil;
import com.common.baselibrary.utils.SystemUtils;

/**
 * Created by zhangjianhui401 on 16/8/23.
 * 基础常量类
 */
public class BaseConstant {

    private static final String TAG = "BaseConstant";

    /**
     * APP包名
     */
    public static final String APP_PACKAGE_NAME = "com.app.mango";

    /**
     * APP内缓存路径根目录
     */
    public static final String BASE_SDCARD_PATH = getBaseCachePath() + "/mango/mango";

    /**
     * APP内缓存根目录
     */
    private static String getBaseCachePath() {
        String cachePath = "";
        try {
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {
                cachePath = Environment.getExternalStorageDirectory().getAbsolutePath();
            } else {//无可用SD卡
                Context context = ApplicationManager.getApplicationContext();
                if (context != null) {
                    cachePath = context.getCacheDir().getPath();

                    if (SystemUtils.isAppProcess(context, context.getPackageName())) {//主进程才处理上报
                        LogUtil.i(TAG, "getBaseCachePath no sdcard which can be used.");
                    } else {
                        LogUtil.i(TAG, "getBaseCachePath no sdcard which can be used but is not main process.");
                    }
                }
            }
        } catch (Exception e) {
            LogUtil.w(TAG, "catch Exception throw by getAppCachePath.", e);
        }
        if (TextUtils.isEmpty(cachePath)) {
            cachePath = "/Android/data/" + APP_PACKAGE_NAME + "/cache";
        }

        LogUtil.i(TAG, "getBaseCachePath cachePath: " + cachePath);
        return cachePath;
    }


}
